package com.pro.exam.web.controller;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.pro.exam.common.utils.DataUtils;
import com.pro.exam.web.model.TransDictPojo;
import com.pro.exam.web.service.DictTransService;
import com.pro.exam.web.service.TranslateService;
@RestController
@RequestMapping("/")
public class TransController {
	@Autowired
	TranslateService translateService;
	@Autowired
	DictTransService dictTransService;
	@Resource(name="redisTemplate")
	private RedisTemplate<String, String> redisTemplate;
	
	/**
	 * 请求翻译的处理接口
	 */
	@RequestMapping("trans")
	public String translateInter(HttpServletRequest request){
		String content=request.getParameter("content");
		String language=request.getParameter("language");
		String callBack = request.getParameter("callback");
		System.out.println("------------------"+content+"------------------");
		JSONArray translateContent = null;
		try {
			translateContent = translateService.translate(language, content);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String transResult = callBack+"({\"trans_result\":"+translateContent.toJSONString()+"})";
		return transResult;
	}
	
	@RequestMapping(value = "sync",method=RequestMethod.GET)
	public String sync(String callback){
		Map<String, HashMap<String, String>> returnMap = new HashMap<String,HashMap<String,String>>();
		Set<String> keys=redisTemplate.opsForSet().members("trans");
		Iterator<String> it = keys.iterator();
		while (it.hasNext()) {
			HashMap<String, String> transMap = new HashMap<String, String>();
			String key = (String) it.next();
			key = key.replaceAll("\"", "");
			String ch1 = redisTemplate.opsForHash().get(key, "ch") == null?null:redisTemplate.opsForHash().get(key, "ch").toString();
			String en1 = redisTemplate.opsForHash().get(key, "en") == null?null:redisTemplate.opsForHash().get(key, "en").toString();
			String jp1 = redisTemplate.opsForHash().get(key, "jp") == null?null:redisTemplate.opsForHash().get(key, "jp").toString();
			transMap.put("ch", ch1);
			transMap.put("en", en1);
			transMap.put("jp", jp1);
			returnMap.put(key, transMap);
		}
		DataUtils.ANALYZER_WORDS = returnMap;
		DataUtils.SWITCH_INITIAL_WORDS_LIB = true;
		System.out.println("同步成功");
		return callback+"({})";
		/*Object s = redisTemplate.opsForHash().get("test", "ch");
		Object s1 = redisTemplate.opsForHash().get("test", "en");
		Object s2 = redisTemplate.opsForHash().get("test", "jp");
		System.out.println(s+""+s1+s2);*/
	}
	
	@RequestMapping(value = "sqlSync", method=RequestMethod.GET)
	public String mysqlSync(String callback){
		//清空redis
		redisTemplate.execute(new RedisCallback() {
            public String doInRedis(RedisConnection connection) throws DataAccessException {
                connection.flushDb();
                return "ok";
            }
        });
		
		//同步mysql和redis
		List<TransDictPojo> list = dictTransService.selectAll();
		for (int i = 0; i < list.size(); i++) {
			TransDictPojo tdp = list.get(i);
			if (tdp.getTranslates() != null) {
				String ch = tdp.getTranslates().get("ch");
				String en = tdp.getTranslates().get("en");
				String jp = tdp.getTranslates().get("jp");
				System.out.println(ch);
				
				HashMap<String,String> map=new HashMap<String,String>();
				map.put("ch", ch);
				map.put("en", en);
				map.put("jp", jp);
				
				redisTemplate.opsForHash().put(ch, "ch", ch);
				redisTemplate.opsForHash().put(ch, "en", en);
				redisTemplate.opsForHash().put(ch, "jp", jp);
				redisTemplate.opsForSet().add("trans", ch);
			};
		}
		
		Map<String, HashMap<String, String>> returnMap = new HashMap<String,HashMap<String,String>>();
		Set<String> keys=redisTemplate.opsForSet().members("trans");
		Iterator<String> it = keys.iterator();
		while (it.hasNext()) {
			HashMap<String, String> transMap = new HashMap<String, String>();
			String key = (String) it.next();
			key = key.replaceAll("\"", "");
			String ch1=(String)redisTemplate.opsForHash().get(key, "ch");
			String en1=(String)redisTemplate.opsForHash().get(key, "en");
			String jp1=(String)redisTemplate.opsForHash().get(key, "jp");
			transMap.put("ch", ch1);
			transMap.put("en", en1);
			transMap.put("jp", jp1);
			returnMap.put(key, transMap);
		}
		DataUtils.ANALYZER_WORDS = returnMap;
		
		return callback+"({})";
	}
}
